﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SGS.Cards;

namespace SGS.Standard
{
    /// <summary>
    /// 系统牌堆对象
    /// </summary>
    public class SystemCardHeap
    {
        /*public SystemCardHeap()
        {
            AllCards = new CardCollection();
            CardsHeap = new CardCollection();
            LostCardsHeap = new CardCollection();
        }*/

        public SystemCardHeap( IEnumerable<Card> cards)
        {
            AllCards = new CardCollection(cards);
            CardsHeap = new CardCollection(cards);
            DiscardsHeap = new CardCollection();
        }

        /// <summary>
        /// 待摸的牌堆
        /// </summary>
        public CardCollection CardsHeap
        {
            get;
            protected set;
        }

        /// <summary>
        /// 弃牌堆
        /// </summary>
        public CardCollection DiscardsHeap
        {
            get;
            protected set;
        }

        /// <summary>
        /// 全部牌（暂未使用）
        /// </summary>
        public CardCollection AllCards
        {
            get;
            protected set;
        }

        public void Shuffle()
        {
            GamePool.Current.RaiseNoticeMessageEvent(this, "Card Heap Shuffle.");
            lock (this)
            {
                if (DiscardsHeap.Count == 0)
                {
                    return;
                }

                DiscardsHeap.Shuffle();
                var dummy = DiscardsHeap.ToList();
                DiscardsHeap.Clear();

                CardsHeap.PushToEnd(dummy);
            }
        }

        public int Count
        {
            get
            {
                return CardsHeap.Count;
            }
        }

        public List<Card> GetRandomCardsForTest(int count = 1)
        {
            List<Card> list = new List<Card>();

            Random r = new Random();
            for (int i = 0; i < count; i++)
            {
                int dummy = r.Next(0, 3);
                if (dummy == 0)
                {
                    list.Add(new Card( Suit.Spade, r.Next(1,14), CardType.Bang ) );
                }
                else
                {
                    list.Add(new Card(Suit.Diamond, r.Next(1, 14), CardType.Miss));
                }
            }

            return list;
        }

        public Card GetCard()
        {
            var r = GetCards();
            if (r.Count == 0)
            {
                return null;
            }
            else
            {
                return r.First();
            }
        }

        public List<Card> GetCards(int count = 1)
        {
            // return GetRandomCardsForTest(count);

            lock (this)
            {
                bool isShuffled = false;
                List<Card> list = new List<Card>();

                for (int i = 0; i < count; i++)
                {
                    Card cb = CardsHeap.PopFirst();

                    if (cb == null)
                    {
                        if (isShuffled)
                        {
                            GameContext.Current.RaiseNoticeMessageEvent(this, "Card Heap is not enough");
                            break;
                        }
                        else
                        {
                            Shuffle();
                            isShuffled = true;
                        }
                    }
                    list.Add(cb);
                }

                return list;
            }
        }

        public List<Card> GetCards( Player p, int count = 1 )
        {
            lock (this)
            {
                bool isShuffled = false;
                List<Card> list = new List<Card>();

                for (int i = 0; i < count; i++)
                {
                    Card cb = CardsHeap.PopFirst();

                    if (cb == null)
                    {
                        if (isShuffled)
                        {
                            GamePool.Current.RaiseNoticeMessageEvent( this, "Card Heap is not enough" );
                            break;
                        }
                        else
                        {
                            Shuffle();
                            isShuffled = true;
                        }
                    }
                    list.Add( cb );
                }

                p.GameInfo.OwnCards.PushToEnd( list );
                GamePool.Current.RaiseNoticeMessageEvent( this, "{0}从牌堆中获得了{1}张牌", p.PlayerName );

                return list;
            }
        }
    }
}
